#!/bin/bash
#
# Copyright (C) 2014 David Goulet <dgoulet@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Save session(s)"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../../

SESSION_NAME="save-42"
CHANNEL_NAME="chan-save"
EVENT_NAME="tp:tptest"

DIR=$(readlink -f $TESTDIR)

NUM_TESTS=41

source $TESTDIR/utils/utils.sh

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

# Expected to fail
function save_lttng_fail()
{
	local sess_name=$1
	local opts=$2

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN save $sess_name $opts >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		ok 0 "Session failed to be saved. Expected!"
	else
		ok 1 "Session has been saved. NOT expected!"
	fi
}

function is_session_saved()
{
	local path=$1
	local name=$2

	[ -f "$path/$name.lttng" ];
	ok $? "Session $name.lttng file found!"
}

function test_basic_save()
{
	diag "Test basic save"

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_ust_lttng_channel_ok $SESSION_NAME $CHANNEL_NAME
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $CHANNEL_NAME
	lttng_track_ok "-p 666 -u -s $SESSION_NAME"

	lttng_save $SESSION_NAME "-o $TRACE_PATH"

	is_session_saved $TRACE_PATH $SESSION_NAME

	destroy_lttng_session_ok $SESSION_NAME
}

function test_basic_save_all()
{
	diag "Test basic save all"

	create_lttng_session_ok $SESSION_NAME-1 $TRACE_PATH
	create_lttng_session_ok $SESSION_NAME-2 $TRACE_PATH
	create_lttng_session_ok $SESSION_NAME-3 $TRACE_PATH

	enable_ust_lttng_channel_ok $SESSION_NAME-1 $CHANNEL_NAME
	enable_ust_lttng_channel_ok $SESSION_NAME-2 $CHANNEL_NAME
	enable_ust_lttng_channel_ok $SESSION_NAME-3 $CHANNEL_NAME

	enable_ust_lttng_event_ok $SESSION_NAME-1 $EVENT_NAME $CHANNEL_NAME
	enable_ust_lttng_event_ok $SESSION_NAME-2 $EVENT_NAME $CHANNEL_NAME
	enable_ust_lttng_event_ok $SESSION_NAME-3 $EVENT_NAME $CHANNEL_NAME

	lttng_track_ok "-p 666 -u -s $SESSION_NAME-1"
	lttng_track_ok "-p 666 -u -s $SESSION_NAME-2"
	lttng_track_ok "-p 666 -u -s $SESSION_NAME-3"

	lttng_save "" "-a -o $TRACE_PATH"

	is_session_saved $TRACE_PATH $SESSION_NAME-1
	is_session_saved $TRACE_PATH $SESSION_NAME-2
	is_session_saved $TRACE_PATH $SESSION_NAME-3

	destroy_lttng_session_ok $SESSION_NAME-1
	destroy_lttng_session_ok $SESSION_NAME-2
	destroy_lttng_session_ok $SESSION_NAME-3
}

function test_overwrite()
{
	diag "Test overwrite session file"

	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_ust_lttng_channel_ok $SESSION_NAME $CHANNEL_NAME
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $CHANNEL_NAME

	lttng_save $SESSION_NAME "-o $TRACE_PATH"

	is_session_saved $TRACE_PATH $SESSION_NAME

	destroy_lttng_session_ok $SESSION_NAME

	# Create a new session with same name so we can force the overwrite
	create_lttng_session_ok $SESSION_NAME $TRACE_PATH
	enable_ust_lttng_channel_ok $SESSION_NAME $CHANNEL_NAME
	enable_ust_lttng_event_ok $SESSION_NAME $EVENT_NAME $CHANNEL_NAME

	# This one MUST fail
	save_lttng_fail $SESSION_NAME "-o $TRACE_PATH"

	# This one MUST succeed
	lttng_save $SESSION_NAME "-f -o $TRACE_PATH"

	is_session_saved $TRACE_PATH $SESSION_NAME

	destroy_lttng_session_ok $SESSION_NAME
}

start_lttng_sessiond

TESTS=(
	test_basic_save
	test_basic_save_all
	test_overwrite
)

for fct_test in ${TESTS[@]};
do
	TRACE_PATH=$(mktemp -d -t tmp.test_save_trace_path.XXXXXX)

	${fct_test}
	if [ $? -ne 0 ]; then
		break;
	fi
	# Only delete if successful
	rm -rf $TRACE_PATH
done

stop_lttng_sessiond
